﻿using System;
using System.Windows;
using System.Windows.Interactivity;

namespace AccessControl.Wpf.Behaviors
{
	class DragDropBehavior : Behavior<FrameworkElement>
	{
		#region Fields

		bool _bIsDragging = false;

		#endregion

		#region Overrides

		protected override void OnAttached()
		{
			base.OnAttached();

			AssociatedObject.MouseLeftButtonDown += (OnAssociatedMouseLeftButtonDown);
			AssociatedObject.MouseMove += (OnAssociatedMouseMove);
			AssociatedObject.MouseLeftButtonUp += (OnAssociatedMouseLeftButtonUp);
		}

		#endregion

		#region Private Members

		void OnAssociatedMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			_bIsDragging = true;			
		}

		void OnAssociatedMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
		{
			if (_bIsDragging)
			{
				_bIsDragging = false;
				DragDrop.DoDragDrop(AssociatedObject, AssociatedObject.DataContext, DragDropEffects.Copy);
			}
		}

		void OnAssociatedMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			_bIsDragging = false;
		}

		#endregion
	}
}
